<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
    String rootPath = request.getContextPath();
    String remoteip= request.getRemoteAddr();
%>
<!DOCTYPE>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>选择物品</title>
<meta name="viewport" content="initial-scale=1, maximum-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="stylesheet"
	href="//g.alicdn.com/msui/sm/0.6.2/css/sm.min.css" />
<link rel="stylesheet"
	href="//g.alicdn.com/msui/sm/0.6.2/css/sm-extend.min.css" />
<link rel="stylesheet" href="../static/weixin.css" />
</head>
<body>
	<header class="bar bar-nav">
	  <h1 class="title">唐泽极充</h1>
	</header>
	<form id="buyfrm" method="post">
		<div class="content">
		<input type="hidden" id="openId" name="openId" value="${openId}" />
		<input type="hidden" id="address" name="memberBuy.addressinfo">
		<input type="hidden" id="username" name="memberBuy.username">
		<input type="hidden" id="userphone" name="memberBuy.userphone">
		<input type="hidden" id="unitprice" name="memberBuy.unitprice" value="${sysMemeberType.unitPrice}">
		  <div class="list-block">
		    <ul>
			 <li class="item-content">
			      <div class="item-inner">
		          <div class="item-title">单价</div>
		          <div class="item-title">￥${sysMemeberType.unitPrice}【${sysMemeberType.typeName}】</div>
		        </div>
	   		  </li>
			 <li class="item-content item-link">
			      <div class="item-inner">
		          <div class="item-title">型号</div>
		          <div class="item-title">
		           <input type="text" id='categoryName' name="memberBuy.categoryName" placeholder="请选择型号" value="48-12方孔"></div>
		        </div>
	   		  </li>
			  <li class="item-content">
			      <div class="item-inner">
		          <div class="item-title">数量</div>
		          <div class="item-title"><input type="text" id='num' name="memberBuy.num" value="1"/></div>
		        </div>
	   		  </li>
	   		  <li class="item-content">
			      <div class="item-inner">
		          <div class="item-title">金额</div>
		          <div class="item-title"><input type="text" id="money" name="memberBuy.money" value="${sysMemeberType.unitPrice}"></div>
		        </div>
	   		  </li>
	   		  <li class="item-content">
			    <div class="item-inner">
		          <div class="item-title">收货信息</div>
		        </div>
	   		  </li>
	   		  
	   		  <li class="item-content" id="divuser">
		        <div class="item-media"><i class="icon icon-f7"></i></div>
		        <div class="item-inner">
		          <div class="item-title" id="divsenduser"></div>
		          <div class="item-after" id="divsenduserphone"></div>
		        </div>
		      </li>
      
	   		  <li class="item-content" id="divuseraddr">
		        <div class="item-media"><i class="icon icon-f7"></i></div>
		        <div class="item-inner" id="divaddress">
		        	<!-- <textarea id="address" name="memberBuy.addressinfo"></textarea> -->
		        	
		        </div>
		        
		      </li>
		      
		      
        
		    
	   		  <li class="align-top">
		        <div class="item-content">
		          <!-- <div class="item-media"><i class="icon icon-form-comment"></i></div> -->
		          <div class="item-inner">
		            <div class="item-title label">备注</div>
		            <div class="item-input">
		            	<input type="text" id="remark" name="memberBuy.remark">
		            </div>
		          </div>
		        </div>
		      </li>
		     </ul>
		    </div>
		    
		 <div class="content-block" id="btn">
		    <div class="row">
		      <div class="col-50"><a href="#" class="button button-big button-fill button-danger">取消</a></div>
		      <div class="col-50" onclick="doBuy();"><a href="#" class="button button-big button-fill button-success">提交</a></div>
		    </div>
		  </div>
		 </div>
	</form>


</body>
   <script type='text/javascript' src='//g.alicdn.com/sj/lib/zepto/zepto.min.js' charset='utf-8'></script>
    <script type='text/javascript' src='//g.alicdn.com/msui/sm/0.6.2/js/sm.min.js' charset='utf-8'></script>
    <script type='text/javascript' src='//g.alicdn.com/msui/sm/0.6.2/js/sm-extend.min.js' charset='utf-8'></script>
    <script type="text/javascript" src="//g.alicdn.com/msui/sm/0.6.2/js/sm-city-picker.min.js" charset="utf-8"></script>
    


<script type="text/javascript">

$(document).ready(function() {
	doShowAddress();
	/* $("#textjquery").click(function(event) {
		alert('您的鼠标移到了 id="p1" 的元素上!');
	}); */    
});

function doShowAddress(){
	var ajaxurl="<%=rootPath%>/json/showAddressOne";
    $.ajax({
             type: "POST",
             url: ajaxurl,//点击登录按钮走的action
             data:{openId:$("#openId").val()},
             async: false,
             dataType: "json",
             success: function(data){
            	 if(data.flag == true ){
            		 if( data.address != null){
            			var addressInfo=data.address.area+data.address.street+data.address.address;
               			$("#divaddress").text(addressInfo);
               			$("#divsenduser").text("收货人："+data.address.username);
               			$("#divsenduserphone").text("联系电话："+ data.address.phone);
               			$("#address").val(addressInfo);
               			$("#username").val(data.address.username);
               			$("#userphone").val(data.address.phone);
            		 }
            		 else{
            			 $("#divuseraddr").hide();
            			 $("#divuser").hide();
            			 $("#btn").hide();
            			 doAlertAddr();
            		}
            	 }
             },
             error: function(data){
            	 alert(data.msg);
             }
         });
}

function doAlertAddr(){
    $.alert('请先添加收货信息', function () {
    	buyfrm.action="<%=rootPath %>/weixin/memberaddaddr?pagesrc=buy";
    	buyfrm.submit();
    });
}

$("#num").picker({
	  toolbarTemplate: '<header class="bar bar-nav">\
	  <button class="button button-link pull-right close-picker" onclick="onPickerNum();">确定</button>\
	  <h1 class="title">请选择数量</h1>\
	  </header>',
	  cols: [
	    {
	      textAlign: 'center',
	      values: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '15', '20']
	    }
	  ]
	});

function onPickerNum(){
	var num =$("#num").val();
	var unitprice=$("#unitprice").val();
	$("#money").val(unitprice * 10000 * num / 10000);
}
	
$("#categoryName").picker({
	toolbarTemplate: '<header class="bar bar-nav">\
	<button class="button button-link pull-right close-picker">确定</button>\
	<h1 class="title">请选择型号</h1>\
	</header>',
	cols: [
	    {
	      textAlign: 'center',
	      values: ['48-12', '48-20', '60-20']
	    },
	    {
	      textAlign: 'center',
	      values: ['方孔', '圆孔']
	    }
  	]
});
	
	
function doBuy(){
	var ajaxurl="<%=rootPath%>/json/buy";
	var categoryName=$("#categoryName").val()+"唐泽极充";
	$.ajax({
            type: "POST",
            url: ajaxurl,//点击登录按钮走的action
            data:{openId:1,address:$("#address").val(),
           	 username:$("#username").val(),
           	 userphone:$("#userphone").val(),
           	 unitprice:$("#unitprice").val(),
           	 categoryName:categoryName,
           	 num:$("#num").val(),
           	 money:$("#money").val(),
           	 remark:$("#remark").val()},
            async: false,
            dataType: "json",
            success: function(data){
           	 if(data.flag == true ){
           		doPay(data.buyid,categoryName,$("#money").val());
           	 }
            },
            error: function(data){
           	 $.alert(data.msg);
            }
        });
}

function doPay(buyid,goods,money){
	$.confirm('现在付款?',function () {
		var ajaxurl="<%=rootPath%>/weixin/pay/weixinpay";
		var spbill_create_ip = "<%=remoteip%>";
		$.ajax({
             type: "POST",
             url: ajaxurl,//点击登录按钮走的action
             async: false,
             data:{buyId:buyid,money:money,categoryname:goods,spbill_create_ip:spbill_create_ip},
             dataType: "json",
             success: function(data){
            	 callpay(data.weixinpay.appId,
            			 data.weixinpay.timeStamp,
            			 data.weixinpay.nonceStr,
            			 data.weixinpay.paypackage,
            			 data.weixinpay.paySign,buyid)
             },
             error: function(data){
		alert("weixin json fail");
            	 $.alert(data.msg);
             }
         });
	},function () {$.alert('您取消了订单');}
	);
}


function callpay(appId,timeStamp,nonceStr,paypackage,paySign,buyid){   
	WeixinJSBridge.invoke('getBrandWCPayRequest',
			{"appId" :appId,"timeStamp" : timeStamp,"nonceStr" : nonceStr,"package" :paypackage,"signType" : "MD5", "paySign" :paySign},
    function(res){
				WeixinJSBridge.log(res.err_msg);
//	     alert(res.err_code + res.err_desc + res.err_msg);
			if(res.err_msg == "get_brand_wcpay_request:ok"){  
				$.alert("微信支付成功!");  
				//跳转待发货页面
				doUpdateOrderPayState(buyid,'1');
			    window.location.href='orderList.html';
			}else if(res.err_msg == "get_brand_wcpay_request:cancel"){  
				$.alert("用户取消支付!");
				doUpdateOrderPayState(buyid,'-2');
				//跳转待支付页面
			}else{  
				$.alert("支付失败!"); 
				doUpdateOrderPayState(buyid,'-1');
				//跳转到首页；
				window.location.href="<%=rootPath%>/weixin/weixinindex";
			}  
 		})
}

function doUpdateOrderPayState(buyid,opeartor){
	var ajaxurl="<%=rootPath%>/json/updatePayState";
	$.ajax({
            type: "POST",
            url: ajaxurl,//点击登录按钮走的action
            data:{paystate:opeartor,buyid:buyid},
            async: false,
            dataType: "json",
            success: function(data){},
            error: function(data){}
        });
}

function doPayTest(buyid){
	 $.confirm('现在付款?',function () {
		 var ajaxurl="<%=rootPath%>/weixin/pay/weixinpayjsp";
		 buyfrm.action=ajaxurl;
		 buyfrm.submit()
	 })
}
</script>
</html>